<?php


namespace app\api\controller\v2;


use app\controller\Base;
use app\model\Baby;
use app\model\Members;
use app\model\Milk;
use app\Request;


class BabyFace extends Base
{
    //第一次登录时，通过自动注册账户获取user_id，同时新增字段BB_ID
    public function autoAddUser(Request $request){
        $data = $request->param();
        $id = Members::create($data)->getData('id');//create返回的是当前模型的对象实例

        return $this->create($id,'自动生成账号成功',200);

    }

    //添加喂养记录，不限制喂养什么，包括吃药等
    public function addMilk(Request $request){
        $data = $request->param();

        //如果传过bb_id=0，则提示添加宝宝，否则直接添加记录
        //$chkBaby = Members::where('id',$data['user_id'])->field('bb_id')->find();
        $result = Milk::create($data);

       return $this->create($result,'添加成功',200);
    }


    //获取所有喂养操作记录
    public function getList(Request $request){

        $data = $request->param();
        $map = [];

        $map[] = ['user_id','=',$data['uid']];

        if ($data['bid']!=0){
            $map[] = ['bb_id','=',$data['bid']];
        }

        //$result = Milk::where('user_id',$data['uid'])->order('id','desc')->select();
        $result = Milk::where($map)->order('id','desc')->select();
        if ($result->isEmpty()){
            return $this->create([],'没有数据',204);
        }

        $rss = [];
        foreach ($result as $key=>$res){

            $res = json_decode($res,true);//先转换成数组，https://www.php.cn/php-ask-487863.html
            $bbname = Baby::where('id',$res['bb_id'])->field('babynike')->find();
            $bbname = json_decode($bbname,true);
            $res['babynike'] = $bbname['babynike'];
            $rss[] = $res;
        }

        //echo json_encode($rss);
        //var_dump($rss);数组不能用echo输出

        return $this->create($rss,'请求成功',200);

    }

    //获取用户的所有宝宝
    public function getBaby(Request $request){
        $uid = $request->param('uid');

        $data = Baby::where('user_id',$uid)->select();

        if ($data->isEmpty()){
            return $this->create(null,'没有数据',400);
        }else{
            return $this->create($data,'请求成功',200);
        }
    }

    //添加宝宝
    public function addBaby(Request $request){

        //获取表单数据
        $data = $request->param();



        $chkData = Baby::where('user_id',$data['user_id'])->find();

        if (empty($chkData)){
            //首次添加宝宝将其设置为默认，即ischoose=1
            $data['ischoose']=1;
        }


        //同一个用户下，宝宝不能同名，一个宝宝只能添加一次，当前用户下检查同名宝宝
        $chkData = Baby::where('user_id',$data['user_id'])
            ->where('babynike',$data['babynike'])
            ->find();

        if (!empty($chkData)){

            return $this->create([],'不能重复添加',400);

        }

        //写入数据并返回ID


//            $res['id'] = $rs;
//            $res['ischoose'] = 1;

            $rs = Baby::create($data)->getData('id');


        if (empty($rs)){
            return $this->create([],'写入失败',204);
        }else{
            if (isset($data['ischoose'])){
                $res['id'] = $rs;
                $res['ischoose'] = 1;
            }else{
                $res['id'] = $rs;
            }
            return $this->create($res,'写入成功',200);
        }

    }

    //编辑宝宝信息
    public function editBaby(Request $request){
        
        $data = $request->param();

        $res = Baby::update($data);
        //静态方法直接更新，$data中包含主键则无需第二参数/查询条件

        if ($res){
            return $this->create($res,'修改成功',200);
        }else{
            return $this->create($res,'修改失败',204);
        }


        //选择默认值，到底用缓存还是数据表字段？

    }

    //获取最近一次喂养记录
    public function getMilk(Request $request){
        $bid = $request->param('bid');

        if ($bid){
            //$res = Milk::where('bb_id',$bid)->order('id','desc')->find();
            $res = Baby::where('id',$bid)->find();
            if (empty($res)){
                return $this->create(null,'参数错误',204);
            }else{
                $data = $res->Milk;
                return $this->create($res,'请求成功',200);
            }
        }else{
            return $this->create(null,'参数错误',204);
        }

    }

    //修改宝宝默认状态
    public function modDefault(Request $request){
        //获取前端传过来的uid和bb_id
        $data = $request->param();


        //通过user_id重置isChoose=0
       $res = Baby::update(['ischoose'=>0],['user_id'=>$data['uid']]);
        //然后根据bb_id修改isChoose=1
        $res = Baby::where('id',$data['bb_id'])->save(['ischoose'=>1]);
        //$res = Baby::update(['ischoose'=>1,'id'=>$data['bb_id']])->getData('id');

        if ($res){
            return $this->create($res,'成功',200);
        }else{
            return $this->create($res,'失败',204);
        }


    }

    //获取宝宝信息
    public function getBabyInfo(Request $request){
        $id = $request->param('bid');
        //halt($id);
        $result = Baby::where('id',$id)->find();
        if (empty($result)){
            return $this->create([],'没有数据',204);
        }else{
            return $this->create($result,'请求成功',200);
        }
    }


    //获取指定BB当天喂养且为有效的记录次数
    public function getNum(Request $request){

        $id = $request->param('bid');

        if ($id){
            $data = Milk::where('bb_id',$id)
                ->where('status',0)
                ->whereDay('create_time')
                ->count('bb_id');

            return $this->create($data,'请求成功',200);
        }else{
            return $this->create([],'参数错误',204);
        }

    }

    //测试关联数据,一个用户对应多个喂养记录
    public function getalllist(Request $request){
        $id = $request->param('uid');

        $result = Members::where('id',$id)->find();

        $data = $result->Milk;

        //$dataA['result'] = $result;

        //$dataA['data'] = $data;

        //halt($data);

        //同时获取用户与喂养记录表关联数据
        return $this->create($result,'ok',200);
    }

    //测试关联数据，宝宝一对多关联喂养记录
    public function getBabyLog(Request $request){
        $id = $request->param('bb_id');
        $result = Baby::where('id',$id)->find();
        $data = $result->Milk;
        return $this->create($result,'请求成功',200);
    }


}